package com.wind.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.wind.entity.Order;
import com.wind.service.IOrderService;
import com.wind.service.IUsersService;
import com.wind.vo.IndexVo;
import com.wind.vo.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;


/**
 * 首页信息
 */
@RestController
@RequestMapping("/home")
public class IndexController {

    @Autowired
    private IUsersService usersService;

    @Autowired
    private IOrderService orderService;

    @GetMapping("/index")
    public Json index() {
        IndexVo indexVo = new IndexVo();
        //用户总数
        indexVo.setUserCount(usersService.selectCount(null));
        //预约咨询
        indexVo.setOrderCount(orderService.selectCount(new EntityWrapper<Order>()
                .eq("create_time", new Date())
                .and().eq("order_type", 2)));
        //测试付款
        indexVo.setTestCount(orderService.selectCount(new EntityWrapper<Order>()
                .eq("create_time", new Date())
                .and().eq("order_type", 1)
                .and().eq("order_status", 2)));
        //待服务订单
        indexVo.setAdvOrderCount(orderService.selectCount(new EntityWrapper<Order>()
                .eq("order_type", 2)
                .and().eq("order_status", 2)));
        //收入金额
        Wrapper<Order> wrapper = new EntityWrapper();
        wrapper.setSqlSelect("sum(money) money");
        wrapper.eq("order_status", 2);
        Map<String, Object> map = orderService.selectMap(wrapper);
        BigDecimal money = new BigDecimal(0);
        if (!Objects.isNull(map) && !Objects.isNull(map.get("money"))) {
            Double tmp = (Double) map.get("money");
            money = new BigDecimal(tmp);
        }
        indexVo.setMoneyCount(money);
        //申请退款
        indexVo.setRefundCount(orderService.selectCount(new EntityWrapper<Order>()
                .and().eq("order_status", 4)));
        return Json.succ().data("data", indexVo);
    }
}
